using System;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
using Exercise.Api.ParamModel;
using Microsoft.IdentityModel.Tokens;
using Exercise.Api.Entity;
using System.Text;

namespace Exercise.Api.Utils
{
    public class TokenHelper
    {
        public static string GenerateToken(TokenParamenter tokenParameter,Users user)
        {
           var claims = new[]
           {
               new Claim(ClaimTypes.Name,user.UserName),
               new Claim(ClaimTypes.Role,"admin"),
           };

           var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenParameter.Secret));
           var credentials =new SigningCredentials(key,SecurityAlgorithms.HmacSha256);
           var jwtToken=new JwtSecurityToken(tokenParameter.Issuer,null, claims,expires:DateTime.UtcNow.AddMinutes(tokenParameter.AccessExpiration),signingCredentials:credentials);

           var token=new JwtSecurityTokenHandler().WriteToken(jwtToken);

           return token;
        }
    }
}